﻿
using System;
using UnityEngine;

public class SelectMgr
{
    public Creature CurTarget;
    private Role _owner;
    private Timer _timer;

    public void Init(Role owner)
    {
        _owner = owner;
        _timer = TimerMgr.GetInstance().CreateTimerAndStart(0.3f, -1, checkDistance);
        
    }

    private void onTargetHpChanged(int hp, int maxHp)
    {
        FightUIMgr.GetInstance().SetTargetInfo(CurTarget.Name, hp, maxHp);
    }

    //检查距离
    private void checkDistance()
    {
        if (CurTarget == null || _owner == null) { return; }
        var dis = Util.Distance2_5D(_owner.transform.position, CurTarget.transform.position);
        if (dis > GameSetting.MaxVisualSenseDis)
        {
            Select(null);
        }
        else
        {
            Select(CurTarget);
        }
    }
    //选择目标
    public void Select(Creature creature)
    {
        if (creature == CurTarget) { return; }
        if (creature != null)
        {
            creature.HPChangedCallback -= onTargetHpChanged;
        }
     
        CurTarget = creature;
        _owner.CurTarget = CurTarget;
        if(creature == null) 
        {
            FightUIMgr.GetInstance().SetTargetActive(false);
            return;
        }
        var dis = Util.Distance2_5D(_owner.transform.position, CurTarget.transform.position);
        if (dis > GameSetting.MaxVisualSenseDis)
        {
            return; 
        }
        creature.HPChangedCallback += onTargetHpChanged;
        FightUIMgr.GetInstance().SetTargetInfo(creature.Name,creature.ServerData.HP,creature.ServerData.MaxHP);
    }
}   

